﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

namespace Heima.OA.Common
{
    public class CDateTime
    {
        public static DateTime GetDateTime(string sInput, string sRegex, int iAddHours, int iWebSiteID)
        {
            DateTime now = DateTime.Now;
            if ((iWebSiteID == 5) || (iWebSiteID == 6))
            {
                now = now.AddHours(-12.0);
            }
            Match match = Regex.Match(sInput, sRegex, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.IgnoreCase);
            if (!match.Success)
            {
                return now;
            }
            int year = now.Year;
            int month = now.Month;
            int day = now.Day;
            int hour = now.Hour;
            int minute = now.Minute;
            if (match.Groups["Month"].Value != "")
            {
                month = int.Parse(match.Groups["Month"].Value);
            }
            if (match.Groups["Day"].Value != "")
            {
                day = int.Parse(match.Groups["Day"].Value);
            }
            if (match.Groups["Hour"].Value != "")
            {
                hour = int.Parse(match.Groups["Hour"].Value);
            }
            if (match.Groups["Minute"].Value != "")
            {
                minute = int.Parse(match.Groups["Minute"].Value);
            }
            try
            {
                now = new DateTime(year, month, day, hour, minute, 0);
            }
            catch
            {
            }
            if ((match.Groups["ap"].Value.ToLower() == "p") || (match.Groups["ap"].Value.ToLower() == "pm"))
            {
                now = now.AddHours(12.0);
            }
            if (((iWebSiteID == 3) && (match.Groups["ap"].Value.ToLower() == "am")) && (match.Groups["Day"].Value == ""))
            {
                now = now.AddDays(1.0);
            }
            if (((iWebSiteID == 4) && (hour < 12)) && (match.Groups["Day"].Value == ""))
            {
                now = now.AddDays(1.0);
            }
            return now.AddHours((double)iAddHours);
        }

        public static void SetDate(DateTime dt)
        {
            SYSTEMTIME systemtime;
            systemtime.year = (short)dt.Year;
            systemtime.month = (short)dt.Month;
            systemtime.dayOfWeek = (short)dt.DayOfWeek;
            systemtime.day = (short)dt.Day;
            systemtime.hour = (short)dt.Hour;
            systemtime.minute = (short)dt.Minute;
            systemtime.second = (short)dt.Second;
            systemtime.milliseconds = (short)dt.Millisecond;
            SetLocalTime(ref systemtime);
        }

        [DllImport("kernel32.dll")]
        private static extern bool SetLocalTime(ref SYSTEMTIME time);

        [StructLayout(LayoutKind.Sequential)]
        private struct SYSTEMTIME
        {
            public short year;
            public short month;
            public short dayOfWeek;
            public short day;
            public short hour;
            public short minute;
            public short second;
            public short milliseconds;
        }

        public static long UNIX_TIMESTAMP(DateTime dt)
        {
            return 0;
        }

        /// <summary>
        /// 获得默认时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDefaultDateTime()
        {
            return Convert.ToDateTime("1900-01-01");
        }
    }
}
